Imports DAL
Imports System.Configuration

Public Class frmLogin
    Public strCallFor As String = "CP"
    Dim blnClose As Boolean
    Dim _blnMDI As Boolean
    'Private blnUpdate As Boolean

    Public ReadOnly Property MDIClose() As Boolean
        Get
            Return _blnMDI
        End Get
    End Property


#Region "SOMETHING EXTRA"
    'L          'LOGIN call at When application starts
    'CP         'CHANGE PASSWORD call from mdi to change password
    'CU         'CHANGE USER call from mdi to change User
#End Region

    Private Sub frmLogin_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed


    End Sub
    
    Private Sub frmLogin_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If Not (ConfigurationManager.AppSettings("FormMode") = "R") Then
            Select Case strCallFor
                Case "L"
                    If Not blnClose Then
                        e.Cancel = False
                        _blnMDI = True
                        'MDIPharmacy.Close()
                    End If

                Case "CP"
                    If isupdate() Then
                        'blnUpdate = False
                        Select Case BCommon.ShowMsgError(10081, "YNC")
                            Case Windows.Forms.DialogResult.Yes
                                If Validate_Data() = True Then
                                    Validate_Password()
                                Else
                                    e.Cancel = True
                                End If
                            Case Windows.Forms.DialogResult.Cancel
                                e.Cancel = True
                            Case Windows.Forms.DialogResult.No
                                'Me.Close()
                        End Select
                    End If
            End Select
        End If
    End Sub

    Private Sub frmLogin_HelpButtonClicked(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.HelpButtonClicked
        'Rauhl_@20073012 : Add following code display help
        If strCallFor = "CP" Then
            'Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Change Password")
        ElseIf strCallFor = "L" Then
            'Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "User Login")
        End If
        e.Cancel = True
    End Sub

    Private Sub frmLogin_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.F1 Then
            'Rauhl_@20073012 : Add following code display help
            e.SuppressKeyPress = True
            If strCallFor = "CP" Then
                'Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Change Password")
            ElseIf strCallFor = "L" Then
                'Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "User Login")
            End If
        End If
    End Sub

    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call DisplayData()
#If DEBUG Then
        txtUsername.Text = "admin"
        txtPassword.Text = "admin"
        cmdOK_Click(Nothing, Nothing)

#End If
        'blnUpdate = False
    End Sub

    Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        Try
            If Validate_Data() = False Then Exit Sub
            Call Validate_Password()
            'blnUpdate = False
            If strCallFor = "CP" Then
                Call ClearData()
                Me.Close()
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "OK_Click")
        End Try
    End Sub

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Try
            If strCallFor = "L" Then

                'LOGIN call at login application only
                blnClose = True
                Me.Close()
                MDINP.Close()
                _blnMDI = True
            Else
                'CHANGE PASSWORD call from mdi to change password OR 'CHANGE USER call from mdi to change User
                ClearData()
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "Cancel_Click")
        End Try
    End Sub

    Private Sub txtPassword_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPassword.KeyDown
        Try
            '@20071113 : Add following Single line
            If e.Alt Then Exit Sub
            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True : Exit Sub
                End If
            End If
            If BCommon.ValidateMiscell(e.KeyCode, "USERID") = False Then    ' 23/10/2007
                e.SuppressKeyPress = True
            End If

            If strCallFor = "CP" Then
                If e.KeyCode = Keys.Enter Then txtnewpwd.Focus()
            Else
                If e.KeyCode = Keys.Enter Then
                    If Validate_Data() = False Then Exit Sub
                    Call Validate_Password()
                    'blnUpdate = False
                End If
                'cmdOK.Focus()
            End If

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtPassword_KeyDown")
        End Try
    End Sub

    Private Sub txtUsername_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtUsername.KeyDown
        Try
            '@20071113 : Add following Single line
            If e.Alt Then Exit Sub
            If e.KeyCode = Keys.Enter Then txtPassword.Focus()
            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True : Exit Sub
                End If
            End If
            If BCommon.ValidateMiscell(e.KeyCode, "USERID") = False Then    ' 23/10/2007
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtUsername_KeyDown")
        End Try
    End Sub

    Private Sub CmDUsername_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Try
            '@20071113 : Add following Single line
            If e.Alt Then Exit Sub
            If e.KeyCode = Keys.Enter Then txtPassword.Focus()
            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True : Exit Sub
                End If
            End If
            If BCommon.ValidateMiscell(e.KeyCode, "USERID") = False Then    ' 23/10/2007
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtUsername_KeyDown")
        End Try
    End Sub

    Private Sub txtnewpwd_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtnewpwd.KeyDown
        Try
            '@20071113 : Add following Single line
            If e.Alt Then Exit Sub
            If e.KeyCode = Keys.Enter Then txtconfirm.Focus()
            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True : Exit Sub
                End If
            End If
            If BCommon.ValidateMiscell(e.KeyCode, "USERID") = False Then    ' 23/10/2007
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtUsername_KeyDown")
        End Try
    End Sub

    Private Sub txtconfirm_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtconfirm.KeyDown
        Try
            '@20071113 : Add following Single line
            If e.Alt Then Exit Sub
            If e.KeyCode = Keys.Enter Then cmdOK.Focus()
            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True : Exit Sub
                End If
            End If
            If BCommon.ValidateMiscell(e.KeyCode, "USERID") = False Then    ' 23/10/2007
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtconfirm_KeyDown")
        End Try

    End Sub

    Private Sub DisplayData()
        Try
            If strCallFor = "CP" Then
                grpbButtons.Tag = "BTN"
            End If
            'Call BCommon.AssignLabels(Me) '    27/07/2007
            Select Case strCallFor
                Case "L"          'LOGIN call at login application only
                    Call BCommon.SetFormStandards(Me, False)
                    txtUsername.Visible = True
                    CmDUsername.Visible = False
                    cmdClose.Visible = False
                    grpbControls.Height = 78
                    cmdOK.Location = New Point(90, 13)
                    cmdCancel.Location = New Point(185, 13)
                    Me.Text = "User Login"
                Case "CP"         'CHANGE PASSWORD call from mdi to change password
                    Call BCommon.SetFormStandards(Me)
                    Me.Text = "Change Password"
                    Me.ControlBox = True
                    cmdClose.Visible = True
                    grpbControls.Height = 145
                    txtnewpwd.Visible = True
                    txtconfirm.Visible = True
                    txtUsername.Visible = False
                    Call PopulateData()
                Case "CU"         'CHANGE USER call from mdi to change User
                    Call BCommon.SetFormStandards(Me)
                    Me.Text = "Change User"
                    grpbControls.Height = 78
            End Select
            grpbButtons.Top = grpbControls.Top + grpbControls.Height + 5
#If Not Debug Then
            Call ClearData()
#End If

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DisplayData")
        End Try
    End Sub

    Private Function isupdate() As Boolean

        If Trim(txtPassword.Text) <> Trim(CStr(txtPassword.Tag)) Or Trim(txtnewpwd.Text) <> Trim(CStr(txtnewpwd.Tag)) _
        Or txtconfirm.Text <> Trim(CStr(txtconfirm.Tag)) Or Trim(txtUsername.Text) <> Trim(CStr(txtUsername.Tag)) _
        Or Trim(CmDUsername.Text) <> Trim(CStr(CmDUsername.Tag)) Then
            Return True
        Else
            Return False
        End If
    End Function
    Private Function Validate_Data() As Boolean
        Try

            If strCallFor = "L" Then
                If Trim(txtUsername.Text) = "" Then
                    BCommon.ShowMsgError(10013)
                    Return ClearTextBox(txtUsername)
                    Return False
                End If
            End If
            If strCallFor = "CP" Then
                If Trim(CmDUsername.Text) = "" Then
                    BCommon.ShowMsgError(10013)
                    Return ClearCombo(CmDUsername)
                    Return False
                End If
            End If
            If Trim(txtPassword.Text) = "" Then
                BCommon.ShowMsgError(10014)
                Return ClearTextBox(txtPassword)
                Return False
            End If

            If strCallFor <> "CP" Then Return True

            If Trim(txtnewpwd.Text) = "" Then
                BCommon.ShowMsgError(10091)
                Return ClearTextBox(txtnewpwd)
                Return False
            End If
            If Trim(txtconfirm.Text) = "" Then
                BCommon.ShowMsgError(10092)
                Return ClearTextBox(txtconfirm)
                Return False
            End If
            If Trim(txtPassword.Text) = Trim(txtnewpwd.Text) Then
                BCommon.ShowMsgError(10145)
                ClearTextBox(txtconfirm)
                Return ClearTextBox(txtnewpwd)
                Return False
            End If
            If Trim(txtnewpwd.Text) <> Trim(txtconfirm.Text) Then
                BCommon.ShowMsgError(10015)
                Return ClearTextBox(txtconfirm)
                Return False
            End If
            Return True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "Validate_Data")
            Return False
        End Try
    End Function

    Private Sub ClearData()
        Try
            CmDUsername.Text = ""
            CmDUsername.Focus()
            txtUsername.Text = ""
            txtUsername.Focus()
            txtconfirm.Text = ""
            txtnewpwd.Text = ""
            txtPassword.Text = ""

            CmDUsername.Tag = CmDUsername.Text
            txtUsername.Tag = txtUsername.Text
            txtconfirm.Tag = txtconfirm.Text
            txtnewpwd.Tag = txtnewpwd.Text
            txtPassword.Tag = txtPassword.Text
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ClearData")
        End Try
    End Sub

    Private Function Validate_Password() As Boolean
        Try
            If strCallFor = "CP" Or strCallFor = "CU" Then
                Select Case DUser.CheckUserPassword(CmDUsername.Text, txtPassword.Text)
                    Case 1
                        BCommon.ShowMsgError(1) 'Invalid userID
                        ClearCombo(CmDUsername) : CmDUsername.Focus() : Return False
                    Case 2
                        BCommon.ShowMsgError(2) 'Invalid Password
                        txtPassword.Clear() : txtPassword.Focus() : Return False
                    Case 3
                        If strCallFor = "CU" Then
                            ConfigurationManager.AppSettings.Set("UserId", CmDUsername.Text)
                        ElseIf strCallFor = "CP" Then
                            If DUser.ChangePassword(CmDUsername.Text, txtconfirm.Text) = True Then BCommon.ShowMsgError(10093)
                        End If
                End Select
            End If
            ' 16/10/2007
            If strCallFor = "L" Then
                Select Case DUser.CheckUserPassword(txtUsername.Text, txtPassword.Text)
                    Case 1
                        BCommon.ShowMsgError(1) 'Invalid userID
                        ClearTextBox(txtUsername) : ClearTextBox(txtPassword) : txtUsername.Focus()
                    Case 2
                        BCommon.ShowMsgError(2) 'Invalid Password
                        ClearTextBox(txtUsername) : ClearTextBox(txtPassword) : txtUsername.Focus()
                    Case 3
                        ConfigurationManager.AppSettings.Set("UserId", txtUsername.Text)
                        blnClose = True
                        Me.Close()
                End Select
            End If
            Return True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "Validate_Password")
        End Try
    End Function

    Private Function ClearTextBox(ByRef Sender As TextBox) As Boolean
        Try
            Sender.Text = ""
            Sender.Focus()
            Return False
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ClearTextBox")
        End Try
    End Function

    Private Function ClearCombo(ByRef Sender As ComboBox) As Boolean
        Try
            Sender.Text = ""
            Sender.Focus()
            Return False
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ClearCombo")
        End Try
    End Function

    Private Sub txtPassword_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPassword.TextChanged
        'blnUpdate = False
    End Sub

    Private Sub txtnewpwd_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtnewpwd.TextChanged
        'blnUpdate = False
    End Sub

    Private Sub txtconfirm_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtconfirm.TextChanged
        'blnUpdate = False
    End Sub
    '
    Private Sub PopulateData()
        Call ClearData()
        Dim dt As DataTable = DUser.GetUserID()
        If dt.Rows.Count > 0 Then
            CmDUsername.DataSource = dt
            CmDUsername.ValueMember = "UserId"
            CmDUsername.DisplayMember = "UserId"
        End If
    End Sub

    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        blnClose = True
        'blnUpdate = True
        'Me.Close()
    End Sub

    Private Sub CmDUsername_KeyDown1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles CmDUsername.KeyDown
        If e.Alt Then Exit Sub
        If e.KeyCode = Keys.Enter Then txtPassword.Focus()
    End Sub

    Private Sub CmDUsername_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'blnUpdate = False
    End Sub
    Private Sub TextBox_GotFocus(sender As Object, e As System.EventArgs) Handles txtPassword.GotFocus, txtUsername.GotFocus
        Dim txtobject As TextBox = sender
        txtobject.BackColor = Color.LightBlue
    End Sub

    Private Sub TextBox_LostFocus(sender As Object, e As System.EventArgs) Handles txtPassword.LostFocus, txtUsername.LostFocus
        Dim txtobject As TextBox = sender
        txtobject.BackColor = Color.White
    End Sub
End Class